package util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import crawler.CrawlerDocument;

public class FileLoader {
	
	public static ArrayList<String> LoadSimpleList(String filename) throws IOException
	{
		ArrayList<String> ret = new ArrayList<String>();
		
		BufferedReader reader = new BufferedReader(new FileReader(filename));
		String line = null;
		while((line = reader.readLine()) != null)
		{
			ret.add(line);
		}
		
		return ret;
	}
	
	public static String LoadContent(String filename) throws IOException{
		String content = "";
		BufferedReader reader = new BufferedReader(new FileReader(filename));
		String line = null;
		while((line = reader.readLine()) != null){
			content += line + "\r\n";
		}
		return content;
	}
	
	public static ArrayList<CrawlerDocument> LoadSeed(String filename) throws IOException{
		
		ArrayList<CrawlerDocument> seedlist = new ArrayList<CrawlerDocument>();
		
		String content = FileLoader.LoadContent(filename);
		
		// TODO : Judge if is empty, empty indicates wrong json format. 
		JSONArray seedArray = (JSONArray)JSONValue.parse(content);
		
		for (int i = 0; i < seedArray.size(); ++i){
			
			JSONObject seedObj = (JSONObject)seedArray.get(i);
			
			String url = (String)seedObj.get("url");
			String xroot = (String)seedObj.get("xpath_root");
			JSONObject xpathsObj= (JSONObject)seedObj.get("xpaths");
			HashMap<String, String> xpaths = new HashMap<String, String>();
			
			for(Iterator<Entry<String, String>> it = xpathsObj.entrySet().iterator();
				it.hasNext();){
				Entry<String, String> element = it.next();
				String key = (String)element.getKey();
				String value = (String)element.getValue();
				xpaths.put(key, value);
			}
		
			CrawlerDocument doc = new CrawlerDocument(url);
			doc.addFeature("xpath_root", xroot);
			doc.addFeature("xpaths", xpaths);
			
			seedlist.add(doc);
		}
		
		return seedlist;
	}
	
	public static Properties LoadProperties(String filename) 
			throws FileNotFoundException, IOException{
		Properties properties = new Properties(); 
		properties.load(new BufferedReader(new FileReader(filename)));
		return properties;
	}
	
}
